home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / comm / tcp / ATCP_src_22.lha / AmiTCP-2.2 / src / amitcp / api / getxbyy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-12  |  11.0 KB  |  475 lines

  1. RCS_ID_C="$Id: getxbyy.c,v 1.10 1993/06/12 22:58:21 too Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  * 
  7.  * Created: Mon Apr 19 12:01:27 1993 too
  8.  * Last modified: Sun Jun 13 01:38:09 1993 too
  9.  *
  10.  * HISTORY
  11.  * $Log: getxbyy.c,v $
  12.  * Revision 1.10  1993/06/12  22:58:21  too
  13.  * Moved allocDataBuffer into allocdatabuffer.[hc]
  14.  *
  15.  * Revision 1.9  1993/06/07  12:37:20  too
  16.  * Changed inet_ntoa, netdatabase functions and WaitSelect() use
  17.  * separate buffers for their dynamic buffers
  18.  *
  19.  * Revision 1.8  1993/06/01  16:37:20  too
  20.  * removed reduntant hostbuf.h include. includes api/gethtbynamadr.h for
  21.  * some prototypes
  22.  *
  23.  * Revision 1.7  1993/05/14  11:34:59  ppessi
  24.  * Fixed NetDataBase locking.
  25.  *
  26.  * Revision 1.7  1993/05/14  11:34:59  ppessi
  27.  * Fixed NetDataBase locking.
  28.  *
  29.  * Revision 1.6  93/05/05  16:09:40  16:09:40  puhuri (Markus Peuhkuri)
  30.  * Fixes for final demo.
  31.  * 
  32.  * Revision 1.5  93/05/04  14:08:54  14:08:54  too (Tomi Ollila)
  33.  * Changed local api function names to the same as the names given
  34.  * to API programmer, but w/ starting _
  35.  * 
  36.  * Revision 1.4  93/05/04  12:12:04  12:12:04  jraja (Jarno Tapio Rajahalme)
  37.  * fix.
  38.  * 
  39.  * Revision 1.3  93/05/02  17:28:52  17:28:52  jraja (Jarno Tapio Rajahalme)
  40.  * Added Obtain & ReleaseSemaphores around NDB usage,
  41.  * moved Forbid()/Permit() inner.
  42.  * 
  43.  * Revision 1.2  93/04/28  13:49:06  13:49:06  too (Tomi Ollila)
  44.  * Moved macro HOSTBUF and inline function stpcpy() to hostbuf.h
  45.  * includes kern/amiga_netdb.h instead of kern/amiga_config.h
  46.  * 
  47.  * Revision 1.1  93/04/27  10:23:20  10:23:20  too (Tomi Ollila)
  48.  * Initial revision
  49.  * 
  50.  *
  51.  */
  52.  
  53. #include <conf.h>
  54.  
  55. #include <sys/param.h>
  56. #include <sys/systm.h>
  57.  
  58. #include <sys/types.h>
  59. #include <sys/socket.h>
  60. #include <netinet/in.h>
  61. #include <netdb.h>
  62.  
  63. #include <kern/amiga_includes.h>
  64.  
  65. #include <api/amiga_api.h>
  66. #include <api/amiga_libcallentry.h>
  67. #include <kern/amiga_netdb.h>
  68. #include <api/allocdatabuffer.h>
  69.  
  70. #include <api/gethtbynamadr.h> /* prototypes (NO MORE BUGS HERE) */
  71.  
  72. int strcasecmp(const char *, const char *);
  73.  
  74. #if __SASC
  75. #define strcasecmp stricmp
  76. #endif
  77.  
  78.  
  79. static long copyGenent(struct SocketBase * libPtr, 
  80.                struct DataBuffer * DB,
  81.                struct GenentNode *ent)
  82. {
  83.   long diff;
  84.  
  85.   if (allocDataBuffer(DB, ent->gn_EntSize) == FALSE) {
  86.     writeErrnoValue(libPtr, ENOMEM);
  87.     return 0;
  88.   }
  89.   
  90.   /*
  91.    * how much to add to old pointers
  92.    */
  93.   diff = (caddr_t)DB->db_Addr - (caddr_t)&ent->gn_Ent;
  94.  
  95.   /*
  96.    * copy given ent verbatim
  97.    */
  98.   bcopy((caddr_t)&ent->gn_Ent, DB->db_Addr, ent->gn_EntSize);
  99.  
  100.   return diff;
  101. }
  102.  
  103.  
  104. /*
  105.  * Host queries if Nameserver is not in use *****************************
  106.  */
  107.  
  108. /*
  109.  * Makehostent() must be called when NDB_Semaphore is obtained.
  110.  */
  111. static struct hostent * makehostent(struct SocketBase * libPtr,
  112.                     struct HostentNode * ent)
  113. {
  114.   long diff;
  115.   short i;
  116.  
  117.   if ((diff = copyGenent(libPtr, &libPtr->hostents,
  118.              (struct GenentNode *)ent)) == 0)
  119.     return NULL; /* failed to allocate memory */
  120.  
  121.   /*
  122.    * patch pointers
  123.    */
  124. #define HOSTENT ((struct hostent *)libPtr->hostents.db_Addr)
  125.   HOSTENT->h_name += diff;
  126.  
  127.   HOSTENT->h_aliases = (char **)((caddr_t)HOSTENT->h_aliases + diff);
  128.   for (i = 0; HOSTENT->h_aliases[i]; i++)
  129.     HOSTENT->h_aliases[i] += diff;
  130.   /* NULL remains null */
  131.  
  132.   HOSTENT->h_addr_list = (char **)((caddr_t)HOSTENT->h_addr_list + diff);
  133.   for (i = 0; HOSTENT->h_addr_list[i]; i++)
  134.     HOSTENT->h_addr_list[i] += diff;
  135.   /* NULL remains null */
  136.  
  137.   return HOSTENT;
  138. #undef HOSTENT
  139. }
  140.  
  141.  
  142. struct hostent * _gethtbyname(struct SocketBase * libPtr,
  143.                   const char * name)
  144. {
  145.   struct HostentNode * entNode;
  146.   struct hostent * host;
  147.   register char **cp;
  148.  
  149.   LOCK_R_NDB(NDB);
  150.  
  151.   for (entNode = (struct HostentNode *)NDB->ndb_Hosts.mlh_Head;
  152.        entNode->hn_Node.mln_Succ;
  153.        entNode = (struct HostentNode *)entNode->hn_Node.mln_Succ)
  154.     if (strcasecmp(entNode->hn_Ent.h_name, (char *)name) == 0)
  155.       goto found;
  156.     else
  157.       for (cp = entNode->hn_Ent.h_aliases; *cp != 0; cp++)
  158.     if (strcmp(*cp, name) == 0)
  159.       goto found;
  160.  
  161.   UNLOCK_NDB(NDB);
  162.   writeErrnoValue(libPtr, 0);
  163.   return NULL;
  164.  
  165.  found:
  166.   host = makehostent(libPtr, entNode);
  167.   UNLOCK_NDB(NDB);
  168.   return host;
  169. }
  170.  
  171.  
  172. struct hostent * _gethtbyaddr(struct SocketBase * libPtr,
  173.                   const char * addr, int len, int type)
  174. {
  175.   struct HostentNode * entNode;
  176.   struct hostent * host;
  177.  
  178.   LOCK_R_NDB(NDB);
  179.   for (entNode = (struct HostentNode *)NDB->ndb_Hosts.mlh_Head;
  180.        entNode->hn_Node.mln_Succ;
  181.        entNode = (struct HostentNode *)entNode->hn_Node.mln_Succ)
  182.     if (entNode->hn_Ent.h_addrtype == type &&
  183.     ! bcmp(entNode->hn_Ent.h_addr, addr, len)) {
  184.       host = makehostent(libPtr, entNode);
  185.       UNLOCK_NDB(NDB);
  186.       return host;
  187.     }
  188.   UNLOCK_NDB(NDB);
  189.   writeErrnoValue(libPtr, 0);
  190.   return NULL;
  191. }
  192.  
  193.  
  194. /*
  195.  * Network queries ****************************************************
  196.  */
  197.  
  198.  
  199. /*
  200.  * Makenetent() must be called when NDB_Semaphore is obtained.
  201.  */
  202. static struct netent * makenetent(struct SocketBase * libPtr,
  203.                   struct NetentNode * ent)
  204. {
  205.   long diff;
  206.   short i;
  207.  
  208.   if ((diff = copyGenent(libPtr, &libPtr->netents,
  209.              (struct GenentNode *)ent)) == 0)
  210.     return NULL;
  211.  
  212.   /*
  213.    * patch pointers
  214.    */
  215. #define NETENT ((struct netent *)libPtr->netents.db_Addr)
  216.   NETENT->n_name += diff;
  217.  
  218.   NETENT->n_aliases = (char **)((caddr_t)NETENT->n_aliases + diff);
  219.   for (i = 0; NETENT->n_aliases[i]; i++)
  220.     NETENT->n_aliases[i] += diff;
  221.   /* NULL remains null */
  222.  
  223.   return NETENT;
  224. #undef NETENT
  225. }  
  226.  
  227.  
  228. struct netent * SAVEDS RAF2(_getnetbyname,
  229.              struct SocketBase *,    libPtr,    a6,
  230.              const char *,        name,    a0)
  231. #if 0
  232. {
  233. #endif
  234.   struct NetentNode * entNode;
  235.   struct netent * net;
  236.   register char **cp;
  237.   
  238.   CHECK_TASK2();
  239.  
  240.   LOCK_R_NDB(NDB);
  241.   for (entNode = (struct NetentNode *)NDB->ndb_Networks.mlh_Head;
  242.        entNode->nn_Node.mln_Succ;
  243.        entNode = (struct NetentNode *)entNode->nn_Node.mln_Succ)
  244.     if (strcmp(entNode->nn_Ent.n_name, name) == 0)
  245.       goto found;
  246.     else
  247.       for (cp = entNode->nn_Ent.n_aliases; *cp != 0; cp++)
  248.     if (strcmp(*cp, name) == 0)
  249.       goto found;
  250.  
  251.   UNLOCK_NDB(NDB);
  252.   writeErrnoValue(libPtr, 0);
  253.   return NULL;
  254.  
  255.  found:
  256.   net = makenetent(libPtr, entNode);
  257.   UNLOCK_NDB(NDB);
  258.   return net;
  259. }
  260.  
  261. struct netent * SAVEDS RAF3(_getnetbyaddr,
  262.              struct SocketBase *,    libPtr,    a6,
  263.              long,            netw,    d0,
  264.              long,            type,    d1)
  265. #if 0
  266. {
  267. #endif
  268.   struct NetentNode * entNode;
  269.   struct netent * net;
  270.  
  271.   CHECK_TASK2();
  272.  
  273.   LOCK_R_NDB(NDB);
  274.   for (entNode = (struct NetentNode *)NDB->ndb_Networks.mlh_Head;
  275.        entNode->nn_Node.mln_Succ;
  276.        entNode = (struct NetentNode *)entNode->nn_Node.mln_Succ)
  277.     if (entNode->nn_Ent.n_addrtype == type && entNode->nn_Ent.n_net == netw) {
  278.       net = makenetent(libPtr, entNode);
  279.       UNLOCK_NDB(NDB);
  280.       return net;
  281.     }
  282.   UNLOCK_NDB(NDB);
  283.   writeErrnoValue(libPtr, 0);
  284.   return NULL;
  285. }
  286.  
  287.  
  288. /*
  289.  * Service queries ****************************************************
  290.  */
  291.  
  292. /*
  293.  * Makeservent() must be called when NDB_Semaphore is obtained.
  294.  */
  295. static struct servent * makeservent(struct SocketBase * libPtr,
  296.                     struct ServentNode * ent)
  297. {
  298.   long diff;
  299.   short i;
  300.  
  301.   if ((diff = copyGenent(libPtr, &libPtr->servents,
  302.              (struct GenentNode *)ent)) == 0)
  303.     return NULL;
  304.  
  305.   /*
  306.    * patch pointers
  307.    */
  308. #define SERVENT ((struct servent *)libPtr->servents.db_Addr)
  309.   SERVENT->s_name += diff;
  310.  
  311.   SERVENT->s_aliases = (char **)((caddr_t)SERVENT->s_aliases + diff);
  312.   for (i = 0; SERVENT->s_aliases[i]; i++)
  313.     SERVENT->s_aliases[i] += diff;
  314.   /* NULL remains null */
  315.  
  316.   SERVENT->s_proto += diff;
  317.  
  318.   return SERVENT;
  319. #undef SERVENT
  320. }  
  321.  
  322. struct servent * SAVEDS RAF3(_getservbyname,
  323.              struct SocketBase *,    libPtr,    a6,
  324.              const char *,        name,    a0,
  325.              const char *,        proto,    a1)
  326. #if 0
  327. {
  328. #endif
  329.   struct ServentNode * entNode;
  330.   struct servent * serv;
  331.   char ** cp;
  332.   
  333.   CHECK_TASK2();
  334.  
  335.   LOCK_R_NDB(NDB);
  336.   for (entNode = (struct ServentNode *)NDB->ndb_Services.mlh_Head;
  337.        entNode->sn_Node.mln_Succ;
  338.        entNode = (struct ServentNode *)entNode->sn_Node.mln_Succ)
  339.     if (strcmp(entNode->sn_Ent.s_name, name) == 0)
  340.       goto found;
  341.     else
  342.       for (cp = entNode->sn_Ent.s_aliases; *cp != 0; cp++)
  343.     if (strcmp(*cp, name) == 0)
  344.       goto found;
  345.  
  346.   UNLOCK_NDB(NDB);
  347.   writeErrnoValue(libPtr, 0);
  348.   return NULL;
  349.   
  350.  found:
  351.   serv = makeservent(libPtr, entNode);
  352.   UNLOCK_NDB(NDB);
  353.   return serv;
  354. }
  355.  
  356. struct servent * SAVEDS RAF3(_getservbyport,
  357.              struct SocketBase *,    libPtr,    a6,
  358.              LONG,            port,    d0,
  359.              const char *,        proto,    a0)
  360. #if 0
  361. {
  362. #endif
  363.   struct ServentNode * entNode;
  364.   struct servent * serv;
  365.   
  366.   CHECK_TASK2();
  367.  
  368.   LOCK_R_NDB(NDB);
  369.   for (entNode = (struct ServentNode *)NDB->ndb_Services.mlh_Head;
  370.        entNode->sn_Node.mln_Succ;
  371.        entNode = (struct ServentNode *)entNode->sn_Node.mln_Succ)
  372.     if (entNode->sn_Ent.s_port == port &&
  373.     (proto == 0 || strcmp(entNode->sn_Ent.s_proto, proto) == 0)) {
  374.       serv = makeservent(libPtr, entNode);
  375.       UNLOCK_NDB(NDB);
  376.       return serv;
  377.     }
  378.  
  379.   UNLOCK_NDB(NDB);
  380.   writeErrnoValue(libPtr, 0);
  381.   return NULL;
  382. }
  383.  
  384.  
  385. /*
  386.  * Protocol queries ****************************************************
  387.  */
  388.  
  389. /*
  390.  * Makeprotoent() must be called when NDB_Semaphore is obtained.
  391.  */
  392. static struct protoent * makeprotoent(struct SocketBase * libPtr,
  393.                       struct ProtoentNode * ent)
  394. {
  395.   long diff;
  396.   short i;
  397.  
  398.   if ((diff = copyGenent(libPtr, &libPtr->protoents,
  399.              (struct GenentNode *)ent)) == 0)
  400.     return NULL;
  401.  
  402.   /*
  403.    * patch pointers
  404.    */
  405. #define PROTOENT ((struct protoent *)libPtr->protoents.db_Addr)
  406.   PROTOENT->p_name += diff;
  407.  
  408.   PROTOENT->p_aliases = (char **)((caddr_t)PROTOENT->p_aliases + diff);
  409.   for (i = 0; PROTOENT->p_aliases[i]; i++)
  410.     PROTOENT->p_aliases[i] += diff;
  411.   /* NULL remains null */
  412.  
  413.   return PROTOENT;
  414. #undef PROTOENT
  415. }  
  416.  
  417. struct protoent * SAVEDS RAF2(_getprotobyname,
  418.                struct SocketBase *,    libPtr,    a6,
  419.                const char *,        name,    a0)
  420. #if 0
  421. {
  422. #endif
  423.   struct ProtoentNode * entNode;
  424.   struct protoent * proto;
  425.   char **cp;
  426.   
  427.   CHECK_TASK2();
  428.  
  429.   LOCK_R_NDB(NDB);
  430.   for (entNode = (struct ProtoentNode *)NDB->ndb_Protocols.mlh_Head;
  431.        entNode->pn_Node.mln_Succ;
  432.        entNode = (struct ProtoentNode *)entNode->pn_Node.mln_Succ)
  433.     if (strcmp(entNode->pn_Ent.p_name, name) == 0)
  434.       goto found;
  435.     else
  436.       for (cp = entNode->pn_Ent.p_aliases; *cp != 0; cp++)
  437.     if (strcmp(*cp, name) == 0)
  438.       goto found;
  439.  
  440.   UNLOCK_NDB(NDB);
  441.   writeErrnoValue(libPtr, 0);
  442.   return NULL;
  443.   
  444.  found:
  445.   proto = makeprotoent(libPtr, entNode);
  446.   UNLOCK_NDB(NDB);
  447.   return proto;
  448. }
  449.  
  450. struct protoent * SAVEDS RAF2(_getprotobynumber,
  451.                struct SocketBase *,    libPtr,    a6,
  452.                long,            protoc,    a0)
  453. #if 0
  454. {
  455. #endif
  456.   struct ProtoentNode * entNode;
  457.   struct protoent * proto;
  458.  
  459.   CHECK_TASK2();
  460.  
  461.   LOCK_R_NDB(NDB);
  462.   for (entNode = (struct ProtoentNode *)NDB->ndb_Protocols.mlh_Head;
  463.        entNode->pn_Node.mln_Succ;
  464.        entNode = (struct ProtoentNode *)entNode->pn_Node.mln_Succ)
  465.     if (entNode->pn_Ent.p_proto == protoc) {
  466.       proto = makeprotoent(libPtr, entNode);
  467.       UNLOCK_NDB(NDB);
  468.       return proto;
  469.     }
  470.  
  471.   UNLOCK_NDB(NDB);
  472.   writeErrnoValue(libPtr, 0);
  473.   return NULL;
  474. }
  475.